/**
 * 房间列表
*/

var wwj_room = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        this.queryInfo();
    },
    queryInfo:function(){
        this.table = $('#tb_wwj_room').bootstrapTable({
            url: contextPath + '/wwj/room/listData.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20, 50, 100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: true,
            searchAlign: 'left',
          /*  toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="agent_mg.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',*/
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'name',
                title: '房间名称',
                align: 'center',
                width: 100
            },{
                field: 'waWaMachine.code',
                title: '设备编号',
                align: 'center',
                width: 100
            }, {
                field: 'waWaMachine.channelName',
                title: '视频渠道名',
                width: 100
            }, {
                field: 'waWaMachine.roomId',
                title: 'roomId',
                width: 100,
                visible:false
            }, {
                field: 'playerNumber',
                title: '在线人数',
                width: 60
            }, {
                field: 'deliverType',
                title: '发货类型',
                width: 60,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '随机发货';
                    }else if(value == 0){
                        return '指定发货';
                    }
                }
            }, {
                field: 'waWaType.typeName',
                title: '娃娃类型',
                width: 60
            }, {
                field: 'waWaMachine.status',
                title: '房间状态',
                width: 60,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '<font color="red">空闲</font>'
                    }else if(value == 2){
                        return '<font color="blue">使用中</font>'
                    }else if(value == 3){
                        return '<font color="green">维护中</font>'
                    }
                }
            },{
                field: 'mode',
                title: '模式',
                align: 'center',
                width: 100,
                formatter:function(value,row,index){
                    if(value == 0){
                        return '正常'
                    }else if(value == 1){
                        return '无限'
                    }
                }
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var strHtml = '';
                        //strHtml += '<a href="javascript:void(0);" onclick="client_list.edit('+row.id+',\''+row.name+'\','+row.ratio+')">编辑</a>&nbsp;&nbsp;';
                    strHtml = '<div class="btn-group">' +
                    '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                    '   <ul class="dropdown-menu">'+
                    '       <li><a href="javascript:void(0);" onclick="wwj_room.edit('+row.id+')">编辑</a></li>'+
                    '   </ul>'+
                    '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "搜索设备编号");
    },
    edit:function(id){
        var editRoomModal = "";
        $.ajax({
            url:contextPath+'/wwj/room/'+id,
            async:false,
            success:function(res){
                var room_name='';
                if(res.data.name != undefined){
                    room_name = res.data.name;
                }

                editRoomModal = '<div id="editRoomModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
                    '   <div class="modal-dialog">'+
                    '       <div class="modal-content">'+
                    '           <div class="modal-header">'+
                    '               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
                    '               <h4 class="modal-title">编辑房间：'+room_name+'</h4>'+
                    '           </div>'+
                    '           <div class="modal-body">' +
                    '               <form class="form-horizontal" id="editRoomForm">'+
                    '                   <input type="hidden" name="id" value="'+id+'">'+
                    '                   <div class="form-group">'+
                    '                       <label class="col-sm-3 control-label">房间名称：</label>'+
                    '                       <div class="col-sm-9">' +
                    '                           <input type="text" class="form-control" name="name" value="'+room_name+'" />' +
                    '                       </div>' +
                    '                   </div>'+
                    '                   <div class="form-group">'+
                    '                       <label class="col-sm-3 control-label">发货类型：</label>'+
                    '                       <div class="col-sm-9">';
                //发货类型
                if(res.data.deliverType == 0) {
                    editRoomModal += '    <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio" name="deliverType" value="1" /><span class="lbl">随机发货</span></lable></div>&nbsp;&nbsp;&nbsp;'+
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio"  name="deliverType" value="0" checked="true" /><span class="lbl">指定发货</span></lable></div>&nbsp;&nbsp;&nbsp;';

                }else{
                    editRoomModal += '    ' +
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px">' +
                        '                       <lable>' +
                        '                           <input type="radio" name="deliverType" value="1" checked="true"/>' +
                        '                           <span class="lbl">随机发货</span>' +
                        '                           </lable>' +
                        '               </div>&nbsp;&nbsp;&nbsp;'+
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px">' +
                        '                       <lable>' +
                        '                               <input type="radio" name="deliverType" value="0" />' +
                        '                               <span class="lbl">指定发货</span>' +
                        '                       </lable>' +
                        '               </div>&nbsp;&nbsp;&nbsp;'+
                        '';
                }
                editRoomModal +='       </div>' +
                    '                   </div>';
                /*结束发货类型*/

                //娃娃类型
                editRoomModal += '' +
                    '           <div  class="form-group" >' +
                    '                <label class="col-sm-3 control-label">娃娃类型：</label>' +
                    '               <div  class="col-sm-9">' +
                    '                   <select name="wawaTypeId" class="form-control">';

                //console.info(res.data.waWaTypeList);
                var type_list = eval(res.data.waWaTypeList);
                $(type_list).each(function(index){
                    var t = type_list[index];
                    if(t.id == res.data.wawaTypeId){
                        editRoomModal += '<option value="'+ t.id+'" selected="selected">'+ t.typeName+'</option>'
                    }else{
                        editRoomModal += '<option value="'+ t.id+'">'+ t.typeName+'</option>'
                    }
                })

                editRoomModal += '' +
                    '                   </select>' +
                    '               </div>' +
                    '           </div>';
                /*娃娃类型结束*/


                /*游戏模式*/
                editRoomModal += '                   <div class="form-group">'+
                '                       <label class="col-sm-3 control-label">游戏模式：</label>'+
                '                       <div class="col-sm-9">';
                //发货类型
                if(res.data.mode == 0) {
                    editRoomModal += '    ' +
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px">' +
                        '                       <lable>' +
                        '                           <input type="radio" name="mode" value="0" checked="true"/>' +
                        '                           <span class="lbl">正常</span>' +
                        '                           </lable>' +
                        '               </div>&nbsp;&nbsp;&nbsp;'+
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px">' +
                        '                       <lable>' +
                        '                               <input type="radio" name="mode" value="1" />' +
                        '                               <span class="lbl">无限</span>' +
                        '                       </lable>' +
                        '               </div>&nbsp;&nbsp;&nbsp;'+
                        '';
                }else if(res.data.mode == 1){
                    editRoomModal += '    <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio" name="mode" value="0" /><span class="lbl">正常</span></lable></div>&nbsp;&nbsp;&nbsp;'+
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio"  name="mode" value="1" checked="true" /><span class="lbl">无限</span></lable></div>&nbsp;&nbsp;&nbsp;';
                }else{
                    editRoomModal += '    <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio" name="mode" value="0" /><span class="lbl">正常</span></lable></div>&nbsp;&nbsp;&nbsp;'+
                        '               <div class="radio" style="margin-top: 0;float:left;margin-right:50px"><lable ><input type="radio"  name="mode" value="1" /><span class="lbl">无限</span></lable></div>&nbsp;&nbsp;&nbsp;';
                }
                editRoomModal +='       </div>' +
                    '                   </div>';


                editRoomModal += '           '+
                    '               </form>'+
                    '           </div>' +
                    '           <div class="modal-footer" style="text-align:center">'+
                    '               <button type="button" class="btn btn-primary"  onclick="wwj_room.editSubmit()" >保存</button>'+
                    '               <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>'+
                    '           </div>'+
                    '       </div>'+
                    '   </div>'+
                    '</div>';
            },
            error:function(){
                alert_message("编辑错误，请联系管理员")
            }
        })


        //console.info(editRoomModal);
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(editRoomModal)[0]);
        $('#editRoomModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#editRoomModal')[0]);
        });
        //wwj_mg.queryInfo();
        //显示
        $("#editRoomModal").modal('show')
    },
    editSubmit:function(){
        $.ajax({
            url:contextPath+'/wwj/room/change',
            data:$('#editRoomForm').serialize(),
            success:function(res){
                wwj_room.table.bootstrapTable('refresh');
                alert_message('保存成功');
            },
            error:function(){
                alert_message('保存错误，请联系管理员');
            }
        });

        //隐藏
        $("#editRoomModal").modal('hide')
    }
});